//
//  ClientDetailTVC.m
//  POS
//
//  Created by sigma xu on 5/6/13.
//  Copyright (c) 2013 sigma xu. All rights reserved.
//

#import "ClientDetailTVC.h"

@interface ClientDetailTVC ()

@end
@implementation ClientDetailTVC
@synthesize id_client;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        //self = [self initWithStyle:style];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    cdbo = [[ClientDBO alloc] init];
//    cdbo = [cdbo get_client_by_id:id_client];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section ==0) {
        return @"Client Company Name";
    }else if (section == 1){
        return @"Client Information";
    }
    return @"";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int numberofrow = 1;
    if (section==1) {
        numberofrow = 5;
    }
    // Return the number of rows in the section.
    return numberofrow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier;
    UITableViewCellStyle style;
    if (indexPath.section == 0) {
        CellIdentifier = @"ClientNameCell";
        style = UITableViewCellStyleDefault;
    }else if (indexPath.section == 1) {
        CellIdentifier = @"ClientDetailCell";
        style = UITableViewCellStyleValue2;
    }else {
        CellIdentifier = @"ClientDetailButtonCell";
        style = UITableViewCellStyleDefault;
    }
    ClientDBO *cdbo = [[ClientDBO alloc] init];
    cdbo = [cdbo get_client_by_id:id_client];
    
    UITableViewCell *cell = [self createCellForIdentifier:CellIdentifier
                                                tableView:tableView
                                                indexPath:indexPath
                                                    style:style
                             ];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn1 setTitle:@"Add To Favorites" forState:UIControlStateNormal];
    btn1.frame = CGRectMake(0, 0,145,40);
    [btn1 addTarget:self action:@selector(button_add_client_to_favorite:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTitle:@"Take Sales Order" forState:UIControlStateNormal];
    btn2.frame = CGRectMake(155, 0,145, 40);
    [btn2 addTarget:self action:@selector(button_take_sales_order:) forControlEvents:UIControlEventTouchUpInside];
    // Configure the cell...
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = cdbo.client_name;
            break;
        case 1:
            if(indexPath.row == 0){
                cell.textLabel.text = @"Code";
                cell.detailTextLabel.text = cdbo.client_code;
            }else if(indexPath.row == 1){
                cell.textLabel.text = @"Contact";
                cell.detailTextLabel.text = cdbo.contact;
            }else if(indexPath.row == 2){
                cell.textLabel.text = @"Tel";
                cell.detailTextLabel.text = cdbo.tel;
            }else if(indexPath.row == 3){
                cell.textLabel.text = @"Fax";
                cell.detailTextLabel.text =cdbo.fax;
            }else if(indexPath.row == 4){
                cell.textLabel.text = @"Address";
                cell.detailTextLabel.text = cdbo.address;
            }
            break;
        case 2:
            //            cell.textLabel.text = @"View Detail of Each Item";
            [cell.contentView addSubview:btn1];
            [cell.contentView addSubview:btn2];
            break;
    }
    if (indexPath.section == 2) {
        //        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        [cell setBackgroundColor:[UIColor clearColor]];
        
    }
    return cell;
}
- (UITableViewCell *)createCellForIdentifier:(NSString *)CellIdentifier
                                   tableView:(UITableView *)tableView
                                   indexPath:(NSIndexPath *)indexPath
                                       style:(UITableViewCellStyle)style{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
        cell = [[UITableViewCell alloc]initWithStyle:style reuseIdentifier:CellIdentifier];
    }
    return cell;
}
- (void)button_add_client_to_favorite:(id)sender{
    ClientDBO *cdbo = [[ClientDBO alloc] init];
    [cdbo add_client_to_favorite:id_client];
    UINavigationController *nav = [self.tabBarController.viewControllers objectAtIndex:2];
    FavoritesTVC *ftvc = (FavoritesTVC *)[nav.viewControllers objectAtIndex:0];
    [ftvc viewDidLoad];
    [ftvc.tableView reloadData];
}
- (void)button_take_sales_order:(id)sender{
    UINavigationController *nav = [self.tabBarController.viewControllers objectAtIndex:0];
    ScanVC *svc = (ScanVC *)nav.topViewController;
//    [svc viewDidLoad];
    ClientDBO *cdbo = [[ClientDBO alloc] init];
    cdbo = [cdbo get_client_by_id:id_client];
    svc.title = cdbo.client_code;
    self.tabBarController.selectedIndex = 0;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
